<template>
   <el-select v-model="value" placeholder="请选择" @scroll="scrollEvent" v-loadmore="loadMore">
    <el-option
      v-for="item in selectOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
</template>
<script>
export default {
  data () {
    return {
      selectOptions: '',
      value: ''
    }
  },
  mounted: function () {
    let arr = []
    for (let i = 0; i < 10000; i++) {
      arr.push({
        value: i,
        label: i
      })
    }
    this.selectOptions = arr.slice(1, 12)
  },
  methods: {
    scrollEvent (e) {
      console.log(e)
    },
    loadMore () {
      console.log('加载更多')
    }
  },
  directives: {
    loadmore: {
      bind (el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdownwrap')
        SELECTWRAP_DOM.addEventListener('scroll', function () {
        /*
        * scrollHeight 获取元素内容高度(只读)
        * scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0.
        * clientHeight 读取元素的可见高度(只读)
        * 如果元素滚动到底, 下面等式返回true, 没有则返回false:
        * ele.scrollHeight - ele.scrollTop === ele.clientHeight;
        */
          const CONDITION = this.scrollHeight - this.scrollTop <= this.clientHeight
          if (CONDITION) {
            binding.value()
          }
        })
      }
    }
  }
}
</script>
<style>

</style>
